/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import view.ViewPrincipal;
import control.ControllerPrincipal;
import javafx.scene.input.MouseEvent;

/**
 * @author Sik
 */

public class MenuManager {

}

public function activeMenu(stage : Integer) : Boolean
{
    var gameScreen = ViewPrincipal.getInstance().getGameScreen();
    var currentPlayer = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getCurrentPlayer();
    activateMenus(gameScreen.innerMenus, false);

    if(ControllerPrincipal.getINSTANCE().getControllerGameBoard().getNbRound() == 0)
    {
        return true;
    }

    if(stage == gameScreen.STRATEGIE.val)
    {
        displayMenus(gameScreen.innerMenus, false);
        gameScreen.innerMenus[gameScreen.STRATEGIE.val].setActive(true);
        activateMenus(gameScreen.strategieMenu, true);

        if(currentPlayer.getEmpire(false) == null)          /* Pas d'empire actif */
        {
            gameScreen.strategieMenu[gameScreen.DECLIN.val].setActive(false);
            gameScreen.strategieMenu[gameScreen.RECUPERER.val].setActive(false);
        }
        else
        {                                 
            if(not((currentPlayer.getNbTroops() - currentPlayer.getAvailableTroops()) > currentPlayer.getEmpire(false).getListProvinces().size()))
            {
                gameScreen.strategieMenu[gameScreen.RECUPERER.val].setActive(false);            /* Troupes récupérables pour attaque */
            }
            gameScreen.strategieMenu[gameScreen.SELECTION.val].setActive(false);
        }
    }
    if(stage == gameScreen.ATTAQUE.val)
    {
        displayMenus(gameScreen.strategieMenu, false);
        displayMenus(gameScreen.innerMenus, true);
        if(currentPlayer.getEmpire(false) != null)
        {
            gameScreen.innerMenus[gameScreen.ATTAQUE.val].setActive(true);
        }
    }
    if(stage == gameScreen.DEFENSE.val)
    {
        if(currentPlayer.getEmpire(false) != null)
        {
            gameScreen.innerMenus[gameScreen.DEFENSE.val].setActive(true);
        }
        var troopsNeeded = ControllerPrincipal.getINSTANCE().goDefense();
        if(troopsNeeded != 0)
        {
            java.lang.System.out.println("Le joueur doit avoir {{troopsNeeded}} troupes disponibles pour pouvoir continuer");
            return false;
        }

        ViewPrincipal.getInstance().getGameScreen().globalUpdateDisplay();
    }
    if(stage == gameScreen.REORGANISATION.val)
    {
        if(ControllerPrincipal.getINSTANCE().getControllerGameBoard().getIdleTroops().size() > 0)
        {
            gameScreen.innerMenus[gameScreen.REORGANISATION.val].setActive(true);
            gameScreen.slots[ControllerPrincipal.getINSTANCE().getControllerGameBoard().getNumCurrentPlayer()].setActive(false);
            ControllerPrincipal.getINSTANCE().getControllerGameBoard().setMemoPlayer(ControllerPrincipal.getINSTANCE().getControllerGameBoard().getCurrentPlayer());
            ControllerPrincipal.getINSTANCE().getControllerGameBoard().setCurrentPlayer(
                    ControllerPrincipal.getINSTANCE().getControllerGameBoard().pickIdleTroopsPlayer());
            gameScreen.slots[ControllerPrincipal.getINSTANCE().getControllerGameBoard().getNumCurrentPlayer()].setActive(true);
        }
        else
        {
            gameScreen.closeTurn();
            
        }
    }
    return true;
}

public function activateMenus(menu : InnerMenu[], active : Boolean) : Void
{
    for(element in menu)
    {
        element.setActive(active);
    }
}

public function displayMenus(menu : InnerMenu[], open : Boolean) : Void
{
    for(element in menu)
    {
        element.visible = open;
    }
}

public function iniActionMenu() : Void
{
    var gameScreen = ViewPrincipal.getInstance().getGameScreen();
    
    
    gameScreen.strategieMenu[gameScreen.DECLIN.val].onMouseClicked = function(e:MouseEvent) : Void
    {
        if(gameScreen.strategieMenu[gameScreen.DECLIN.val].isActive)
        {
            var currentPlayer = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getCurrentPlayer();
            var civToDepress = currentPlayer.getEmpire(false).getCivilization();

            ControllerPrincipal.getINSTANCE().goDepress(currentPlayer);
            ViewPrincipal.getInstance().getGameScreen().slots[ControllerPrincipal.getINSTANCE().getControllerGameBoard().getNumCurrentPlayer()].setCivilization(false, null);
            ViewPrincipal.getInstance().getGameScreen().slots[ControllerPrincipal.getINSTANCE().getControllerGameBoard().getNumCurrentPlayer()].setCivilization(true, civToDepress);
            ViewPrincipal.getInstance().getGameScreen().globalUpdateDisplay();

            gameScreen.closeTurn();
        }
    }

    gameScreen.strategieMenu[gameScreen.SELECTION.val].onMouseClicked = function(e:MouseEvent) : Void
    {
        if(gameScreen.strategieMenu[gameScreen.SELECTION.val].isActive)
        {
            gameScreen.civilizationPicker.magnified();
            gameScreen.civilizationPicker.setActive(true);
        }
    }

    gameScreen.strategieMenu[gameScreen.RECUPERER.val].onMouseClicked = function(e:MouseEvent) : Void
    {
        if(gameScreen.strategieMenu[gameScreen.RECUPERER.val].isActive)
        {
            var currentPlayer = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getCurrentPlayer();
            currentPlayer.getEmpire(false).mobilizeMaximumTroops();
            ViewPrincipal.getInstance().getGameScreen().globalUpdateDisplay();
        }
    }

}

